package com.generico.dao.ctg;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.generico.base.BaseDaoImpl;
import com.generico.dto.ctg.CtgProducto;
import com.generico.dto.ctg.CtgProveedor;
import com.generico.dto.ctg.CtgSubTipoSucursal;
import com.generico.dto.ctg.CtgSucursal;
import com.generico.dto.ctg.CtgTipoSucursal;
import com.generico.exception.AsiWebException;
import com.main.cliente.dto.Pedido;
import com.main.java.DocumentoAdjunto;


@Repository
public class CtgProveedorDaoImpl extends BaseDaoImpl implements CtgProveedorDao{
	
	
	@Autowired
	private SessionFactory sessionFactory;

	private Session openSession() {
		return sessionFactory.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> getAllActivesAsArray() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(CtgProveedor.class);
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgProveedorId")).
				add(Projections.property("ctgProveedorNombre"))
				);
		criteria.addOrder(Order.asc("ctgProveedorNombre"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	public CtgProveedor encontrarCtgProveedorPorId(Long ctgProveedorId) throws AsiWebException {
		CtgProveedor ctgProveedor = new CtgProveedor();
		ctgProveedor = (CtgProveedor) findById(CtgProveedor.class, ctgProveedorId);
		return ctgProveedor;	
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> obtenerProveedorActivos() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(CtgProveedor.class);
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgProveedorId")).
				add(Projections.property("ctgProveedorNombre")).
				add(Projections.property("ctgProveedorDireccion")).
				add(Projections.property("ctgProveedorEmail")).
				add(Projections.property("ctgProveedorCiudad")).
				add(Projections.property("ctgProveedorIdentificacion")).
				add(Projections.property("ctgProveedorTelefono")).
				add(Projections.property("ctgProveedorNoReg")).
				add(Projections.property("ctgProveedorEstado"))
				);
		criteria.addOrder(Order.asc("ctgProveedorNombre"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	public void guardarProveedor (CtgProveedor proveedor) throws AsiWebException{
		save(proveedor);
	}
	
}
